package com.YxyBackMange.StatcManger;


import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Stack;

public class XMLUtil {
    private File file;

    public XMLUtil(String path) {
        file = new File(path);

    }

    public XMLUtil(File file) {
        this.file = file;
    }

    public XMLUtil() {
    }

    public static void main(String[] args) throws DocumentException, URISyntaxException {
        String str = XMLUtil.class.getClassLoader().getResource("").toURI().getPath();
        str += "config" + File.separatorChar + "static-config.xml";
        XMLUtil xmlUtil = new XMLUtil(str);
        List<StaticConfig> list = xmlUtil.getConfig();
        System.out.println(list);
    }

    public File getFile() {
        return file;
    }

    public void setFile(File file) {
        this.file = file;
    }

    public List<StaticConfig> getConfig() {
        List<StaticConfig> list = new Stack<StaticConfig>();
        try {

            SAXReader reader = new SAXReader();
            org.dom4j.Document document = null;
            document = reader.read(file);
            Element root = document.getRootElement();
            List<Element> elements = root.elements("bean");
            for (Element el : elements) {
                StaticConfig staticConfig = new StaticConfig();
                List<Element> es = el.elements("property");
                String name = el.attribute("name").getText().toString();
                if (name == null || name.equals("")) {
                    throw new Exception("没有找到bean 的name属性");
                } else {
                    staticConfig.setName(name);
                }
                for (Element ee : es) {
                    String proname = ee.attribute("name").getText();
                    if (proname.equals("generate")) {
                        UrlPar urlPar = new UrlPar();
                        try {
                            urlPar.setUrl(ee.attribute("url").getText());
                            String parstring = ee.getText();
                            if (parstring != null) {
                                String[] par_array = parstring.split(";");
                                for (String str : par_array) {
                                    urlPar.getPars().add(str);
                                }
                            }

                        } catch (Exception e) {
                            throw new Exception("静态化地址url未定义");
                        }
                        staticConfig.setGenerateUrl(urlPar);
                    }
                    if (proname.equals("refresh")) {
                        UrlPar urlPar = new UrlPar();
                        try {
                            urlPar.setUrl(ee.attribute("url").getText());
                            String parstring = ee.getText();
                            if (parstring != null) {
                                String[] par_array = parstring.split(";");
                                for (String str : par_array) {
                                    urlPar.getPars().add(str.trim());
                                }
                            }
                        } catch (Exception e) {
                            throw new Exception("静态化地址url未定义");
                        }
                        staticConfig.getRefreshUrls().add(urlPar);
                    }
                    if (proname.equals("prefix")) {
                        if (ee.getText() != null && !ee.getText().equals(""))
                            staticConfig.setPrefix(ee.getText());
                    }
                    if (proname.equals("suffix")) {
                        if (ee.getText() != null && !ee.getText().equals(""))
                            staticConfig.setSuffix(ee.getText());
                    }
                    if (proname.equals("interval")) {
                        if (ee.getText() != null && !ee.getText().equals("")) {
                            staticConfig.setInterVal(Long.parseLong(ee.getText()));
                        }
                    }
                }
                list.add(staticConfig);
            }

        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }


}
